package com.easyapi.web.controller.project;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyapi.common.contants.Constants;
import com.easyapi.common.enums.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.common.param.project.AddProjectParam;
import com.easyapi.common.param.project.ProjectOperationParam;
import com.easyapi.common.param.project.ProjectPageParam;
import com.easyapi.common.param.project.ProjectSearchParam;
import com.easyapi.common.utils.BaseResponse;
import com.easyapi.common.utils.RegularUtil;
import com.easyapi.common.utils.ResultUtils;
import com.easyapi.common.vo.datasource.DataSourceListVo;
import com.easyapi.common.vo.project.ProjectListVo;
import com.easyapi.common.vo.project.ProjectVo;
import com.easyapi.server.service.api.ProjectService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(Constants.BASE_URL + "project")
@Api(tags = {"EProject"})
public class ProjectController {
    @Resource
    private ProjectService projectService;

    @PostMapping("/saveProject")
    public BaseResponse<Boolean> saveProject(@RequestBody AddProjectParam param) {
        String projectName = param.getProjectName();
        String projectDesc = param.getProjectDesc();
        if (StringUtils.isAnyBlank(projectDesc, projectName)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        projectService.saveProject(param);
        return ResultUtils.success(true);
    }

    @PostMapping("pageProject")
    public BaseResponse<Page<ProjectVo>> pageProject(@RequestBody ProjectPageParam param) {
        Long createBy = param.getCreateBy();
        Long updateBy = param.getUpdateBy();
        if (Objects.nonNull(createBy) && createBy <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (Objects.nonNull(updateBy) && updateBy <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        Page<ProjectVo> page = projectService.page(param);
        return ResultUtils.success(page);
    }

    @PostMapping("/addProjectDatasource")
    public BaseResponse<Boolean> addProjectDatasource(@RequestBody ProjectOperationParam param) {
        Long projectId = param.getProjectId();
        Long datasourceId = param.getDatasourceId();
        if (Objects.isNull(projectId) || Objects.isNull(datasourceId)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (projectId <= 0 || datasourceId <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        projectService.addProjectDatasource(projectId, datasourceId);
        return ResultUtils.success(true);
    }

    @PostMapping("/delProjectDatasource")
    public BaseResponse<Boolean> removeProjectDatasource(@RequestBody ProjectOperationParam param) {
        if (Objects.isNull(param.getProjectId()) || param.getProjectId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (Objects.isNull(param.getDatasourceId()) || param.getDatasourceId() <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        projectService.removeProjectDatasource(param.getProjectId(), param.getDatasourceId());
        return ResultUtils.success(true);
    }

    @PostMapping("listProject")
    public BaseResponse<List<ProjectListVo>> listProject(@RequestBody ProjectSearchParam param) {
        if (RegularUtil.StrsLengthError(2, 255, param.getProjectName())) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<ProjectListVo> projectListVos = projectService.listProject(param.getProjectName());
        return ResultUtils.success(projectListVos);
    }
}
